﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class BytesMapObjectInfoRepository : BaseRepository<BytesMapObjectInfo>, IBytesMapObjectInfoRepository
    {
        public BytesMapObjectInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<PageDto<BytesMapObjectInfoDto>> GetBytesMapObjectInfoAsync(Expression<Func<BytesMapObjectInfo, PlcReadDbInfo, bool>> exp, PageModel page)
        {
            PageDto<BytesMapObjectInfoDto> pageDto = new PageDto<BytesMapObjectInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<BytesMapObjectInfo, PlcReadDbInfo>((a, b) => new JoinQueryInfos(
                JoinType.Left, a.PlcReadDbId == b.ID))
                .Where(exp)
                .Select((a, b) => new BytesMapObjectInfoDto
                {
                    ID = a.ID,
                    PlcReadDbId = a.PlcReadDbId,
                    BytesIndex = a.BytesIndex,
                    BytesLength = a.BytesLength,
                    ObjectLength = a.ObjectLength,
                    ObjectName = a.ObjectName,
                    DbCode = b.DbCode,
                    ProcessModuleType = b.ProcessModuleType,
                    ProcessModuleName = b.ProcessModuleName,
                    EquipmentCode=b.EquipmentCode,
                    PlcName = b.PlcName
                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);

            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }
    }
}
